package com.julis.init;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.equipment.LogEntity;
import com.thread.OffLineThread;
import com.util.AnalysisConfiguration;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by juli on 2020/3/25.
 */

public class Init_HF {

    public static String SCJConfigPath = Environment
            .getExternalStorageDirectory() + "/scj";
    public static String SCJConfiguration = "SCJConfiguration.xml";
    // 维护日记的序号
    public static long NUM = 0;
    // 存放日记内容的Map
    public static Map<String, List<LogEntity>> context = null;
    // 所有配置文件名称
    public static Map<String, String> map = new HashMap<String, String>();

    public static boolean isWord = false;

    public static int SType;


    // 终端物理串号(终端机编号)
    public static String TERMINIMEI = "";
    // 测试时间
    public static long time;
    public static int KeyType = 6;
    // 定义返回键
    public static String backKey = "17";
    // 定义确认键
    public static String enterKey = "2F";
    // 激活键
    public static String activeKey = "37";
    // 检测键
    public static String detectKey = "3B";
    // 左侧按钮
    public static String initKey = "3E";
    // 右侧按钮
    public static String personalKey = "3D";
    public static String activeKey2 = "00";

    public static void InitParam() {
        map.put("address", "10.36.0.57");
        map.put("port", "8445");
        map.put("address2", "10.36.0.57");
        map.put("port2", "8445");
        map.put("url", "192.168.236.130:58694");
        map.put("TxChannel", "0");
        map.put("RxChannel", "0");
        map.put("TxPower", "24");
        map.put("RxPower", "24");
        map.put("CRC", "1");
        map.put("PsamNumForVersion1", "1");
        map.put("PsamNumForVersion2", "2");
        map.put("PsamNumForVersion3", "3");
        map.put("PsamNumForVersion4", "4");
        map.put("BstNumber", "200"); // 50 40 20 20
        map.put("BstInterval", "20");
        map.put("TrasferChannelNumber", "100");
        map.put("TrasferChannelInterval", "10");
        map.put("IsModifySignedDate", "0");
        map.put("SignedDate", "20000101");
        map.put("IsModifyExpiredDate", "0");
        map.put("ExpiredM", "0");
        map.put("ExpiredN", "0");
        map.put("IsExpiredN", "0");
        map.put("City", "浙江");
        map.put("PassWord", "21232f297a57a5a743894a0e4a801fc3");// admin密码
        map.put("KeyType", "6");
        map.put("Protocol", "1");// 0:老协议 1:新协议
        try {
            writeFile();
        } catch (Exception e) {

        }
        KeyType = Integer.parseInt(map.get("KeyType"));
        if (KeyType == 4) {
            backKey = "3B";
            enterKey = "17";
            personalKey = "3D";
            detectKey = "37";
            activeKey = "2F";
            activeKey2 = "3E";
            initKey = "00";
        } else {
            KeyType = 6;
            backKey = "17";
            enterKey = "2F";
            personalKey = "3D";
            detectKey = "3B";
            activeKey = "37";
            activeKey2 = "00";
            initKey = "3E";
        }
        Log.e("test", "static KeyType=" + KeyType + ",enter=" + enterKey
                + ",activeKey=" + activeKey);
    }

    public static void init(Context context) {
        InitParam();
        initRoot();
        powerOn();
        OffLineThread offLine = new OffLineThread("A9_2", context);
        offLine.start();
    }

    private static BufferedWriter CtrlFile;
    public static String power_on_write = "1";

    // 孙琦加的
    private static void powerOn() {
        try {
            String path = "/sys/class/gpio/gpio969/value";
            File DeviceName = new File(path);
            CtrlFile = new BufferedWriter(new FileWriter(DeviceName, false));
            CtrlFile.write(power_on_write);
            CtrlFile.flush();
            // powerStatus.setBackgroundColor(Color.GREEN);
            // Toast.makeText(LoginActivity.this, "上电成功", Toast.LENGTH_SHORT)
            // .show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取所有配置文件并检查文件是否存在 不存在就用默认值创建配置文件
     */
    public static void writeFile() {
        Map<String, String> configMap = AnalysisConfiguration
                .readConfig(SCJConfiguration);
        if (AnalysisConfiguration.compareMap(configMap, map) == false) {
            Log.d("test", "map not equals");
            AnalysisConfiguration.deleteConfig(SCJConfiguration);
            AnalysisConfiguration.WriteXmlStr(SCJConfiguration, map);
        } else {
            Log.d("test", "configmap");
            map = configMap;
        }
    }

    private static void initRoot() {
        String path = "/sys/class/gpio/gpio969/value";
        PrintWriter PrintWriter = null;
        Process process = null;
        try {
            process = Runtime.getRuntime().exec("su");
            PrintWriter = new PrintWriter(process.getOutputStream());
            PrintWriter.flush();
            PrintWriter.close();
            int value = process.waitFor();
            // Toast.makeText(getApplicationContext(), "获取ROOT权限成功", 0).show();
            // return returnResult(value);
        } catch (Exception e) {
            // Toast.makeText(getApplicationContext(), "获取ROOT权限失败", 0).show();
            e.printStackTrace();
        } finally {
            if (process != null) {
                process.destroy();
            }
        }
        File DeviceName = new File(path);
        try {
            CtrlFile = new BufferedWriter(new FileWriter(DeviceName, false));
        } catch (IOException e3) {
            e3.printStackTrace();
        }
    }
}
